<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>poi上传</title>
        <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
        <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/JsonExportExcel.min.js"></script>
        <script type="text/javascript" src="/webapp/js/common/config.js"></script>
    </head>
    <body>
        <div class="easyui-container-fluid">
            <div class="easyui-panel" style="border: none;">
                <div id="nav" class="nav">
                </div>
                <div class="easyui-panel" style="width:100%;border:none;">
                    <form id="ff">
                        <div style="margin-bottom:20px;">
                            <form id="fileForm" action="" enctype="multipart/form-data"method="post">
                                <label class="item-label">选择文件：</label>
                                <input id="file" multiple="multiple" style="width:230px;"/>
                                <a href="javascript:void(0);" class="easyui-linkbutton c6" style="margin-left:40px;width:80px;" onclick="fileUpload()">上传</a>
                            </form>
                            <a href="javascript:void(0);" class="easyui-linkbutton c8" style="margin-left:40px;width:80px;" onclick="searchBy()">查询</a>
                        </div>
                    </form>
                    <div style="margin-bottom:10px;">
                    <label>
                        注意:上传文件格式必须是[A:店铺名称][B:店铺地址][C:渠道信息][D:经纬度][E:距离][F:片区编码][G:样本编码]，且第一行必须是标题
                    </label></div>
                </div>
                <div class="table-wrapper">
                    <table id="datagrid" data-options="fitColumns:true,scrollbarSize:0">
                        <thead>
                            <tr>
                                <th data-options="field:'code',align:'center'" width="80">文件码</th>
                                <th data-options="field:'fileName',align:'center'" width="160">文件名</th>
                                <th data-options="field:'uploadNum',align:'center'" width="80">上传总数</th>
                                <th data-options="field:'uploadStatus',align:'center',formatter:uploadStatusFormatter" width="100">状态</th>
                                <th data-options="field:'createTime',align:'center'" width="100">上传时间</th>
                                <th data-options="field:'updateTime',align:'center'" width="100">修改时间</th>
                                <th data-options="field:'action',align:'center',formatter:actions" width="200">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </body>
    <script type="text/javascript">
        $(function(){
            setNav();
            $("#file").filebox({
                buttonText: "选择文件",
                multiple: false   //是否支持多选

            });
            var height = $(window).height();
            var nav_height = $("#nav")._outerHeight();
            var ff_height = $("#ff").height();
            var table_height = height-nav_height-ff_height-40;
            $("#datagrid").css("max-height",table_height);
            $("#datagrid").datagrid({
                pagination:true,
                rownumbers:true,
                checkOnSelect:false,
                onBeforeSelect:function (index,row) {
                    return false;
                }
            }).datagrid("getPager").pagination({
                onSelectPage:searchBy
            });
            searchBy();
        });

        function searchBy() {
            $.ajax({
                url:config.url + "/shop/getUploadFileRecord",
                type:"get",
                xhrFields:{withCredentials:true},
                beforeSend:function () {
                    $.messager.progress({
                        title:"请稍等",
                        interval:50,
                        text:"数据加载中..."
                    });
                },
                complete:function () {
                    $.messager.progress("close");
                },
                success:function(data){
                    $("#datagrid").datagrid("loadData", data.msg);
                    $.messager.progress("close");
                },
                error:function(data){
                    $.messager.progress("close");
                }
            });
        }

        function uploadStatusFormatter(val,row) {
            if (row.uploadStatus == 1) {
                return "上传中";
            }else if (row.uploadStatus == 0) {
                return "上传完成";
            }
            return "--";
        }

        function actions(val,row) {
            return "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' " +
                "style='width:60px;' onclick='goin(\"" + row.code + "\")'>进入</a>&nbsp;&nbsp;&nbsp;&nbsp;" +
                "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' " +
                "style='width:60px;' onclick='exportData(\"" + row.code + "\")'>导出</a>&nbsp;&nbsp;&nbsp;&nbsp;" +
            "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' " +
            "style='width:120px;' onclick='uploadResult(\"" + row.code + "\")'>查看上传结果</a>&nbsp;&nbsp;&nbsp;&nbsp;" +
            "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' " +
            "style='width:60px;' onclick='deleteUpload(\"" + row.code + "\")'>删除</a>";
        }

        function fileUpload() {
            var value = $("#file").filebox('getValue');
            if (!value) {
                $.messager.alert("提示", "请选择文件", "info");
                return;
            }
            var file = $("#file").next().find('input[type=file]')[0].files[0];
            if (!file || !file.name.endsWith("xlsx")) {
                $.messager.alert("提示", "请选择正确的xlsx文件", "info");
                return;
            }
            var form = new FormData()
            form.append("file", file);
            $.messager.confirm("提示", "您确定要上传该文件吗？", function (flag) {
                if (flag) {
                    $.ajax({
                        url: config.url + "/shop/uploadFile",
                        type: "post",
                        data:form,
                        xhrFields: {withCredentials: true},
                        dataType:'json',
                        processData:false,
                        contentType:false,
                        beforeSend: function () {
                            $.messager.progress({
                                title: "请稍等",
                                interval: 30,
                                text: "数据加载中..."
                            });
                        },
                        success: function (data) {
                            $.messager.progress("close");
                            if (data.code == 200) {
                                $.messager.alert("提示", data.msg, "info",function () {
                                    searchBy();
                                });
                            }else{
                                $.messager.alert("提示", data.msg, "error");
                            }
                        }
                    })
                }
            })
        }

        function exportData(code) {
            $.messager.confirm("提示", "您确定要导出数据吗？", function (flag) {
                if (flag) {
                    $.ajax({
                        url: config.url + "/shop/export",
                        type: "post",
                        data:{uploadRecordCode:code},
                        xhrFields: {withCredentials: true},
                        dataType:'json',
                        beforeSend: function () {
                            $.messager.progress({
                                title: "请稍等",
                                interval: 30,
                                text: "数据加载中..."
                            });
                        },
                        success: function (data) {
                            $.messager.progress("close");
                            if (data.code == 200) {
                                let msg = data.msg;
                                var option = {};
                                var content = data.msg.content;
                                var sheetHeader = ['店铺名称(原)', '店铺地址(原)', '渠道信息(原)', '经纬度(原)','片区编码','样本编码',
                                    '店铺名称(真实)', '店铺地址(真实)', '经纬度(真实)', '类型',"城市","中心点距离","数据来源"];
                                var sheetData = [" "];    //要给默认值，不然为空会报错
                                if (content) {
                                    for (var i = 0; i < content.length; i++) {
                                        let infoSources = content[i].infoSources;
                                        if (infoSources) {
                                            if (infoSources == 1) {
                                                infoSources = "腾讯";
                                            }else if (infoSources == 2) {
                                                infoSources = "高德";
                                            }
                                        }
                                        var row = [content[i].shopName ? content[i].shopName : " ",
                                            content[i].address ? content[i].address : " ",
                                            content[i].channelInfo ? content[i].channelInfo : " ",
                                            content[i].lonAndLat ? content[i].lonAndLat : " ",
                                            content[i].areaCode ? content[i].areaCode : " ",
                                            content[i].sampleCode ? content[i].sampleCode : " ",
                                            content[i].trueShopName ? content[i].trueShopName : " ",
                                            content[i].trueAddress ? content[i].trueAddress : " ",
                                            content[i].trueLonAndLat ? content[i].trueLonAndLat : " ",
                                            content[i].type ? content[i].type : " ",
                                            content[i].city ? content[i].city : " ",
                                            content[i].distance ? content[i].distance : " ",
                                            infoSources,
                                        ];
                                        sheetData[i] = row;
                                    }
                                }
                                let fileName = msg.fileName.substring(0, msg.fileName.length - 5);
                                option.fileName = fileName+"(确认)";
                                option.datas = [
                                    {
                                        sheetHeader: sheetHeader,
                                        sheetData: sheetData,
                                        sheetName: '详细信息',
                                    },
                                ];
                                var toExcel = new ExportJsonExcel(option);
                                toExcel.saveExcel();
                            }else {
                                $.messager.alert("提示", data.msg, "info");
                            }
                        }
                    })
                }
            })
        }

        function goin(code){
            window.parent.addTab("poi操作","/webapp/pages/poi/operate.html?code=" + code);
        }

        function uploadResult(code) {
            $.ajax({
                url: config.url + "/shop/getUploadResult",
                type: "get",
                data:{code: code},
                xhrFields: {withCredentials: true},
                dataType:'json',
                success: function (data) {
                    if (data.code == 200) {
                        if (data.msg) {
                            $.messager.alert("提示", data.msg, "info");
                        }else {
                            $.messager.alert("提示", "无", "info");
                        }
                    }else{
                        $.messager.alert("提示", data.msg, "error");
                    }
                }
            })
        }

        function deleteUpload(code) {
            $.messager.confirm("提示", "您确定要删除此文件和数据吗？", function (flag) {
                if (flag) {
                    $.ajax({
                        url: config.url + "/shop/delete/" + code,
                        type: "delete",
                        xhrFields: {withCredentials: true},
                        dataType: 'json',
                        success: function (data) {
                            if (data.code == 200) {
                                $.messager.alert("提示", data.msg, "info",function () {
                                    searchBy();
                                });
                            } else {
                                $.messager.alert("提示", data.msg, "error");
                            }
                        }
                    })
                }
            });
        }
    </script>
</html>